package com.ark.config;

import com.ark.constant.DocConstant;
import com.ark.interceptor.JwtTokenAdminInterceptor;
import com.ark.interceptor.JwtTokenUserInterceptor;
import com.ark.properties.ResourcesProperties;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
@RequiredArgsConstructor
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    private final JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    private final ResourcesProperties resourcesProperties;
    private final JwtTokenUserInterceptor jwtTokenUserInterceptor;


    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/employee/login");
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/user/login")
                .excludePathPatterns("/user/shop/status");
    }

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public OpenAPI customOpenAPI() {

        Contact contact = new Contact();
        contact.name(DocConstant.AUTHOR);
        contact.email(DocConstant.EMAIL);
        return new OpenAPI()
                .info(new Info()
                        .contact(contact)
                        .title(DocConstant.TITLE)
                        .version(DocConstant.VERSION)
                        .description(DocConstant.DESCRIPTION)
                );
    }




    /**
     * 设置静态资源映射
     * @param registry 注册器
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        //TODO 这里为了方便，就直接充当资源服务器了
        registry.addResourceHandler("/img/**").addResourceLocations("file:/"+System.getProperty("user.dir")+resourcesProperties.getPath());
    }

    /*
     * 扩展mvc框架的消息转换器
     * 和knife4j有冲突，暂时不用
     * @param converters 转换器
     */
//    @Override
//    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//        log.info("开始扩展消息转换器...");
//        //创建一个消息转换器对象
//        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//        //设置对象转换器，可以将Java对象转为json字符串
//        converter.setObjectMapper(new JacksonObjectMapper());
//
//        //将自己的转换器放入spring mvc容器中,index为优先使用
//        converters.add(0,converter);
//    }
}
